﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
//using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.VisualBasic;
using System.Collections;
using System.Globalization;
using System.Windows.Documents;
using System.Collections.ObjectModel;
using Microsoft.Windows.Controls;

namespace Projet
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        Bdd bdd = new Bdd();
        CurrentUser currentUser = new CurrentUser();
        int privilegeInfo;
        public Window1()
        {
            InitializeComponent();
        }

        private void test_Click(object sender, RoutedEventArgs e)
        {   
            int id = Convert.ToInt32(UserId.Text);
            
            if (currentUser.FillUserInfos(id) == true)
            {
                 if (bdd.LogMe(id, Password.Password) == true)
                {
                     
                    currentUser.FillUserInfos(id);
                    CultureInfo myCI = new CultureInfo("en-US");
                    System.Globalization.Calendar myCal = myCI.Calendar;
                    int curweek = myCal.GetWeekOfYear(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day), CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
                    InfosWeek.Content = curweek;
                    InfosYear.Content = DateTime.Now.Year;
                    InfosClasse.Content = currentUser.classe;
                    privilegeInfo = currentUser.privilege;
                    // accès selon les droits de l'utilisateur:
                   
                    timeTableGrid.ItemsSource = bdd.GetPlanning(currentUser.campus,currentUser.cursus,curweek,currentUser.classe,DateTime.Now.Year);

                    if (currentUser.privilege == 4)
                    {
                        adminTab.IsEnabled = false;
                        
                    }
                    if (currentUser.privilege == 3)
                    {
                        adminTab.IsEnabled = true;
                        manageUsers.IsEnabled = false;
                        manageCampusTab.IsEnabled = false;
                        manageCursusTab.IsEnabled = false;
                        manageClassesTab.IsEnabled = false;
                        createPlanning.IsEnabled = false;
                        comboBoxManagePlanningCursus.ItemsSource = bdd.getCursus(); // affiche la liste des cursus dans le combobox de manage planning tab
                        comboBoxManagePlanningCursus.SelectedItem = currentUser.cursus; // uniquement droit de modification sur son cursus
                        comboBoxManagePlanningCursus.IsEnabled = false;
                        comboBoxManagePlanningCampus.ItemsSource = bdd.GetCampus(); // affiche la liste des campus dans le combobox de manage planning tab
                        comboBoxManagePlanningCampus.SelectedItem = currentUser.campus; // uniquement droit de modification sur son campus
                        comboBoxManagePlanningCampus.IsEnabled = false;
                        comboBoxYear.SelectedItem = DateTime.Now.Year - 2010;
                        comboBoxWeek.SelectedIndex = curweek;
                        timeTableGridAdmin.IsReadOnly = false;

                    }
                    if (currentUser.privilege == 2)
                    {
                        
                        adminTab.IsEnabled = true;
                        manageUsers.IsEnabled = true;
                        manageCampusTab.IsEnabled = true;
                        manageClassesTab.IsEnabled = true;
                        createPlanning.IsEnabled = true;
                       
                        CampusBox.ItemsSource = bdd.GetCampus();
                        CampusBox.SelectedItem = currentUser.campus; // selection du campus
                        CampusBox.IsEnabled = false; // et on ne peut pas le changer
                        CampusList.ItemsSource = bdd.GetCampusMembers(currentUser.campus);
                        manageCursusTab.IsEnabled = false; // on ne peut pas ajouter de cursus, seul l'admin le peut
                        comboBoxManagePlanningCursus.ItemsSource = bdd.getCursus(); // affiche la liste des cursus dans le combobox de manage planning tab
                        comboBoxManagePlanningCampus.ItemsSource = bdd.GetCampus(); // affiche la liste des campus dans le combobox de manage planning tab
                        comboBoxManagePlanningCampus.SelectedItem = currentUser.campus; // uniquement droit de modification sur son campus
                        comboBoxManagePlanningCampus.IsEnabled = false;
                        comboBoxYear.SelectedItem = DateTime.Now.Year - 2010;
                        comboBoxWeek.SelectedIndex = curweek;
                        manageClassesCursusBox.ItemsSource = bdd.GetCursusFromCampus(currentUser.campus); // liste cursus dans manage classes tab
                        manageClassesCampusBox.ItemsSource = bdd.GetCampus(); // liste campus dans manage classes tab
                        Button_DeleteCampus.IsEnabled = false;
                        timeTableGridAdmin.IsReadOnly = false;
                        addCampusButton.IsEnabled = false;
                        
                    }
                    if (currentUser.privilege == 1)
                        {
                            button1.IsEnabled = true;
                            adminTab.IsEnabled = true;
                            manageUsers.IsEnabled = true;
                            manageCampusTab.IsEnabled = true;
                            manageClassesTab.IsEnabled = true;
                            createPlanning.IsEnabled = true;
                            manageCursusTab.IsEnabled = true;
                            //timeTableGrid.IsReadOnly = false; // edition du planning général
                            CampusBox.IsReadOnly = false;
                            Button_DeleteCampus.IsEnabled = true;
                            comboBoxManagePlanningCampus.IsEnabled = true;
                            addCampusButton.IsEnabled = true;
                            UsersDataGrid.ItemsSource = bdd.getLastUserId(); // affiche les derniers users dans la datagrid de la tab manage User
                            CampusBox.ItemsSource = bdd.GetCampus(); // Liste des campus dans CampusBox de manage campus tab
                            addCampusButton.IsEnabled = true; // Active le bouton "create campus" de manage campus tab
                            manageLabelCampus.ItemsSource = bdd.GetCampus(); // liste campus dans manage users tab
                            manageLabelCursus.ItemsSource = bdd.getCursus(); // liste des cusus dans manage user tab
                            CampusList1.ItemsSource = bdd.getCursus(); //liste des cursus dans manage cusus tab
                            manageClassesCursusBox.ItemsSource = bdd.getCursus(); // liste cursus dans manage classes tab
                            manageClassesCampusBox.ItemsSource = bdd.GetCampus(); // liste campus dans manage classes tab
                            comboBoxManagePlanningCursus.ItemsSource = bdd.getCursus(); // affiche la liste des cursus dans le combobox de manage planning tab
                            comboBoxManagePlanningCampus.ItemsSource = bdd.GetCampus(); // affiche la liste des campus dans le combobox de manage planning tab
                            comboBoxYear.SelectedItem = DateTime.Now.Year - 2010; // Selectionne l'année actuelle dans le combobox de manage planning tab
                            comboBoxClass.SelectedIndex = curweek; // selectionne la semaine actuelle dans le combobox de manage planning tab
                            timeTableGridAdmin.IsReadOnly = false;
                        }
                    TimeTableTab.IsEnabled = true;
                    TimeTableTab.Focus();
                    //selectionComboBox.DataContext = bdd.GetMatieres();
                }

                else { MessageBox.Show("Wrong password"); }
               
            }
            else { MessageBox.Show("Id does not exists"); }
        }   // Tester si l'utilisateur existe

        private void manageUserId_Click(object sender, RoutedEventArgs e)
        {

            if (bdd.GetUserInfo(Convert.ToInt32(manageUserId.Text)) != null ) {
            string[] infos = new string[4];
            infos = bdd.GetUserInfo(Convert.ToInt32(manageUserId.Text));
            manageLabelCampus.Text =  infos[2];
            manageLabelName.Text =  infos[0];
            manageLabelFirstName.Text =   infos[1];
            manageLabelPrivilege.Content = "Privilege: " + infos[3];
            manageDeleteUser.IsEnabled = true;
            manageChangePrivilege.IsEnabled = true;
            manageLabelCampus.IsEnabled = true;
            manageLabelName.IsReadOnly = false;
            manageLabelFirstName.IsReadOnly = false;
            manageLabelCursus.IsEnabled = true;
            manageLabelClasses.IsEnabled = true;
            manageLabelPassword.IsReadOnly = false;
            manageLabelName.BorderBrush = new SolidColorBrush(Colors.Pink);
            manageLabelFirstName.BorderBrush = new SolidColorBrush(Colors.Pink);
            manageCommit.IsEnabled = true;
            
            }
            else 
            {
                MessageBox.Show("User does not exists");
                manageDeleteUser.IsEnabled = false;
                manageChangePrivilege.IsEnabled = false;
                manageLabelCampus.IsEnabled = false;
                manageLabelName.IsReadOnly = true;
                manageLabelFirstName.IsReadOnly = true;
                manageLabelPassword.IsReadOnly = true;
                manageLabelCursus.IsEnabled = false;
                manageLabelClasses.IsEnabled = false;
                manageCommit.IsEnabled = false;
            }
        }

        private void manageDeleteUser_Click(object sender, RoutedEventArgs e)
        {
            bdd.DeleteUser(Convert.ToInt32(manageUserId.Text)); 
            
            MessageBox.Show("User Deleted");
            manageDeleteUser.IsEnabled = false;
            manageChangePrivilege.IsEnabled = false;
        }

        private void manageCommit_Click(object sender, RoutedEventArgs e)
        {
            if (manageLabelPassword.Text.Length >= 10 || manageLabelPassword.Text == "")
            {
              
                bdd.ModifyUser(Convert.ToInt32(manageUserId.Text), manageLabelName.Text, manageLabelFirstName.Text, manageLabelCampus.SelectedItem.ToString(), manageChangePrivilege.SelectedIndex, manageLabelPassword.Text, manageLabelCursus.SelectedItem.ToString(), manageLabelClasses.SelectedIndex);
                manageLabelPassword.Text = "";
                UsersDataGrid.ItemsSource = bdd.getLastUserId();
                MessageBox.Show("User has been modified");
                manageDeleteUser.IsEnabled = false;
                manageChangePrivilege.IsEnabled = false;
                manageLabelCampus.IsEnabled = false;
                manageLabelName.IsReadOnly = true;
                manageLabelFirstName.IsReadOnly = true;
                manageLabelPassword.IsReadOnly = true;
                manageCommit.IsEnabled = false;
                manageLabelCursus.IsEnabled = false;
                manageLabelClasses.IsEnabled = false;       
                manageLabelName.BorderBrush = new SolidColorBrush(Colors.AliceBlue);
                manageLabelFirstName.BorderBrush = new SolidColorBrush(Colors.AliceBlue);
                manageLabelPrivilege.Content = "Privilege: " + (manageChangePrivilege.SelectedIndex+1).ToString();

            }
            else { MessageBox.Show("Pass must have 10 characters minimum"); }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            AddUser window = new AddUser();
            window.Initialize(currentUser);
            window.Show(); 
        }

        private void CampusBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (CampusBox.SelectedItem == null)
            {
                campusName.Content = "Please select a Campus";
            }
            else
            {
                campusName.Content = "Campus name: " + CampusBox.SelectedItem.ToString();
                Button_DeleteCampus.IsEnabled = true;
                string[] infos = new string[2];
                try
                {
                    infos = bdd.GetCampusManager(CampusBox.SelectedItem.ToString());
                    if (infos == null) { infos[0] = ""; infos[1] = ""; }
                    campusManager.Content = "Campus manager: " + infos[0] + infos[1];
                    CampusList.ItemsSource = bdd.GetCampusMembers(CampusBox.SelectedItem.ToString());
                }
                catch
                {
                    campusManager.Content = "Campus manager: - ";
                    CampusList.ItemsSource = bdd.GetCampusMembers(CampusBox.SelectedItem.ToString());
                }
            }
        }

        private void timeTableGrid_BeginningEdit(object sender, Microsoft.Windows.Controls.DataGridBeginningEditEventArgs e)
        {
            TextBlock t = e.Column.GetCellContent(e.Row) as TextBlock;
            t.Background = textBlockColor.Background;
        }

        private void timeTableGrid_CellEditEnding(object sender, Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs e)
        {
            TextBox t = e.EditingElement as TextBox;
            int colonne = e.Column.DisplayIndex;
            //int hour = e.Row.GetIndex() + 1;
            Week w = new Week();
            string hour = "";
            if (timeTableGrid.SelectedIndex >= 0 && timeTableGrid.SelectedIndex <= 14)
            {
                int h = 7;
                
                if (timeTableGrid.SelectedIndex <= 4) { hour = (timeTableGrid.SelectedIndex + h) + "-" + (timeTableGrid.SelectedIndex + h + 1); }
                else if (timeTableGrid.SelectedIndex == 5) {  hour = "12-1"; }
                else { hour = (timeTableGrid.SelectedIndex - 5).ToString() + "-" + (timeTableGrid.SelectedIndex - 4).ToString(); }
                
            }
           
            string modification = t.Text;
            // e.Row.Background = new SolidColorBrush(Colors.Gold);
            //t.Background = textBlockColor.Background;
           // bdd.ModifyPlan(hour, colonne, modification,);
        }

        private void addCampusButton_Click(object sender, RoutedEventArgs e)
        {
            string campus = Interaction.InputBox("New Campus' name ?", "Add New Campus", "", -1, -1);
            bdd.AddCampus(campus);
            MessageBox.Show("New Campus created");
            CampusBox.ItemsSource = bdd.GetCampus();
            manageClassesCampusBox.ItemsSource = bdd.GetCampus();
            manageLabelCampus.ItemsSource = bdd.GetCampus();
            comboBoxManagePlanningCampus.ItemsSource = bdd.GetCampus();
        }

        private void UsersDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            UsersDataGrid.ItemsSource = bdd.getLastUserId();
            manageDeleteUser.IsEnabled = true;
            manageChangePrivilege.IsEnabled = true;
            manageLabelCampus.IsEnabled = true;
            manageLabelName.IsReadOnly = false;
            manageLabelFirstName.IsReadOnly = false;
            manageLabelPassword.IsReadOnly = false;
            manageLabelCursus.IsEnabled = true;
            manageLabelClasses.IsEnabled = true;
          
            manageLabelName.BorderBrush = new SolidColorBrush(Colors.Pink);
            manageLabelFirstName.BorderBrush = new SolidColorBrush(Colors.Pink);
            manageCommit.IsEnabled = true;
            
            foreach (NewUsers u in e.AddedItems)
            {
                manageLabelName.Text = u.name.ToString();
                manageLabelFirstName.Text = u.firstname.ToString();
                manageUserId.Text = u.id.ToString();
                if (u.campus == null)
                {
                    manageLabelCampus.Text = "";
                }
                else
                {
                    manageLabelCampus.Text = u.campus.ToString();
                }
                manageLabelCursus.Text = u.cursus.ToString();
                manageLabelClasses.Text = u.classe.ToString();
                manageLabelPrivilege.Content = "Privilege: " + u.privilege.ToString();
               
            }
        }


        private void timeTableGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void Button_DeleteCampus_Click(object sender, RoutedEventArgs e)
        {
            bdd.DeleteCampus(CampusBox.SelectedItem.ToString());
            MessageBox.Show("Campus " + CampusBox.Text + " deleted");
            CampusBox.ItemsSource = bdd.GetCampus();
            comboBoxManagePlanningCampus.ItemsSource = bdd.GetCampus();
            
        }

        //liste qui affiche les cursus (1)
       private void CursusList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
           CampusList2.ItemsSource = "";
           Button_DeleteCursus.IsEnabled = true;
           CampusList2.ItemsSource = bdd.GetCampusFromCursus(CampusList1.SelectedItem.ToString());
           SubjectList.ItemsSource = null;
        }

        private void CampusListFromCursus_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CampusList4.ItemsSource = "";
            CampusList3.ItemsSource = "";
            try { CampusList3.ItemsSource = bdd.GetClassesFromCampus(CampusList2.SelectedItem.ToString(), CampusList1.SelectedItem.ToString()); }
            catch { }
        }

        private void CampusList3_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CampusList4.ItemsSource = "";
            try 
            { 
                CampusList4.ItemsSource = bdd.GetUsersFromClasse(Convert.ToInt32(CampusList3.SelectedItem), CampusList2.SelectedItem.ToString(), CampusList1.SelectedItem.ToString());
                SubjectList.ItemsSource = bdd.GetTeacher(CampusList2.SelectedItem.ToString(),CampusList3.SelectedIndex);
            }
            catch { }
        }

        private void addCursusButton_Click(object sender, RoutedEventArgs e)
        {
            string cursus = Interaction.InputBox("Add a cursus", "Add Cursus", "", -1, -1);
            if (cursus != "") {
                bdd.Cursus("add", cursus);
                CampusList1.ItemsSource = bdd.getCursus();
                manageClassesCursusBox.ItemsSource = bdd.getCursus();
                manageLabelCursus.ItemsSource = bdd.getCursus();
                comboBoxManagePlanningCursus.ItemsSource = bdd.getCursus();
            }
            else { MessageBox.Show("Campus name empty"); }
        }

        private void Button_DeleteCursus_Click(object sender, RoutedEventArgs e)
        {
            try { 
                bdd.Cursus("delete", CampusList1.SelectedItem.ToString());
                CampusList1.ItemsSource = bdd.getCursus();
                comboBoxManagePlanningCursus.ItemsSource = bdd.getCursus();
            }
            catch { }
        }

        private void createPlanning_Click(object sender, RoutedEventArgs e)
        {
            CreatePlanning window = new CreatePlanning();
            window.Show();
           
        }

        private void find_Click(object sender, RoutedEventArgs e)
        {
            try {
                if (bdd.GetPlanning(comboBoxManagePlanningCampus.SelectedItem.ToString(), comboBoxManagePlanningCursus.SelectedItem.ToString(), comboBoxWeek.SelectedIndex, comboBoxClass.SelectedIndex, comboBoxYear.SelectedIndex + 2010) != null)
                {
                    timeTableGridAdmin.ItemsSource = timeTableGridAdmin.ItemsSource = bdd.GetPlanning(comboBoxManagePlanningCampus.SelectedItem.ToString(), comboBoxManagePlanningCursus.SelectedItem.ToString(), comboBoxWeek.SelectedIndex, comboBoxClass.SelectedIndex, comboBoxYear.SelectedIndex + 2010);
                    List<string> subjectList = bdd.GetSubjects().ToList<string>();
                    subjectList.Add("Custom");
                    comboBoxSubject.ItemsSource = subjectList;
                    comboBoxSubject.SelectedIndex = 1;
                }
                else { MessageBox.Show("This week do not exist, please click on 'Create planning' button before continue"); }
            }
            catch { MessageBox.Show("Missing parameters"); }
        }

        private void timeTableGridAdmin_CellEditEnding(object sender, Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs e)
        {
            {
                Week w = new Week();
                string hour = "";
                if (timeTableGridAdmin.SelectedIndex >= 0 && timeTableGrid.SelectedIndex <= 14)
                {
                    int h = 7;

                    if (timeTableGridAdmin.SelectedIndex <= 4) { hour = (timeTableGridAdmin.SelectedIndex + h) + "-" + (timeTableGridAdmin.SelectedIndex + h + 1); }
                    else if (timeTableGridAdmin.SelectedIndex == 5) { hour = "12-1"; }
                    else { hour = (timeTableGridAdmin.SelectedIndex - 5).ToString() + "-" + (timeTableGridAdmin.SelectedIndex - 4).ToString(); }
                }          
                int column = e.Column.DisplayIndex;
                try
                {
                    TextBox t = e.EditingElement as TextBox;
                    string modification;
                    if (comboBoxSubject.Text == "Custom" || comboBoxSubject.Text == "")
                    {
                        modification = t.Text;
                        if (bdd.PrivilegesTest(hour, column, modification, comboBoxManagePlanningCampus.Text, comboBoxManagePlanningCursus.Text, comboBoxClass.SelectedIndex, comboBoxWeek.SelectedIndex, comboBoxYear.SelectedIndex + 2010, privilegeInfo) == true)
                        {
                            if (modification != "" && modification != null)
                            {
                                bdd.ModifyPlan(hour, column, privilegeInfo + ":" + modification, comboBoxManagePlanningCampus.Text, comboBoxManagePlanningCursus.Text, comboBoxClass.SelectedIndex, comboBoxWeek.SelectedIndex, comboBoxYear.SelectedIndex + 2010);
                            }
                        }
                        else
                        {
                            MessageBox.Show("You do not have the right privileges to change the value of this cell");
                        }
                    }
                    else
                    {
                        modification = comboBoxSubject.Text;
                        if (bdd.PrivilegesTest(hour, column, modification, comboBoxManagePlanningCampus.Text, comboBoxManagePlanningCursus.Text, comboBoxClass.SelectedIndex, comboBoxWeek.SelectedIndex, comboBoxYear.SelectedIndex + 2010, privilegeInfo) == true)
                        {
                            bdd.ModifyPlan(hour, column, privilegeInfo + ":" + modification, comboBoxManagePlanningCampus.Text, comboBoxManagePlanningCursus.Text, comboBoxClass.SelectedIndex, comboBoxWeek.SelectedIndex, comboBoxYear.SelectedIndex + 2010);
                            t.Text = comboBoxSubject.Text;
                        }
                        else
                        {
                            MessageBox.Show("You do not have the right privileges to change the value of this cell");
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Erreur : " + ex.ToString());
                }
                //int hour = e.Row.GetIndex() + 1;



            }
        }

        private void next_week_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            
           int week = Convert.ToInt32(InfosWeek.Content);
            int year = Convert.ToInt32(InfosYear.Content);
            if (week == 52) { week = 0; year = year + 1;  }
            InfosYear.Content = year.ToString();
            InfosWeek.Content = (week + 1).ToString();
            timeTableGrid.ItemsSource = bdd.GetPlanning(currentUser.campus, currentUser.cursus, week+1,currentUser.classe, year);
        }

        private void last_week_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            int week = Convert.ToInt32(InfosWeek.Content);
            int year = Convert.ToInt32(InfosYear.Content);

            if (week == 1) { week = 53; year = year - 1; }
                
                InfosYear.Content = year.ToString();
                InfosWeek.Content = (week - 1).ToString(); 
            
                timeTableGrid.ItemsSource = bdd.GetPlanning(currentUser.campus, currentUser.cursus, week -1, currentUser.classe, year);
        }

        private void manageClassesCursusBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (currentUser.privilege == 1) { manageClassesCursusBox.ItemsSource = bdd.getCursus(); }
            else if (currentUser.privilege == 2) { manageClassesCursusBox.ItemsSource = bdd.GetCursusFromCampus(currentUser.campus); }
            manageClasseCampus.Visibility = Visibility.Visible;
            manageClassesCampusBox.Visibility = Visibility.Visible;
            manageClassesCampusBox.ItemsSource = bdd.GetCampusFromCursus(manageClassesCursusBox.SelectedItem.ToString());
            if (currentUser.privilege == 2) { 
                manageClassesCampusBox.SelectedItem = currentUser.campus;
                manageClassesCampusBox.IsEnabled = false;
            }
        }

        private void manageClassesCampusBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            manageClasse.Visibility = Visibility.Visible;
            manageClassesClassBox.Visibility = Visibility.Visible;
            manageClassesClassBox.SelectedIndex = 0;
            try
            {
                UserListe.ItemsSource = bdd.GetUsersFromClasse(0, manageClassesCampusBox.SelectedItem.ToString(), manageClassesCursusBox.SelectedItem.ToString());
                TeacherList.ItemsSource = bdd.GetTeacher(manageClassesCampusBox.SelectedItem.ToString(), 0);
            }
            catch { }
            SubjectBox.ItemsSource = bdd.GetSubjects();
            manageSubjects.Visibility = Visibility.Visible;
        }

        private void manageClassesClassBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try {
                UserListe.ItemsSource = bdd.GetUsersFromClasse(manageClassesClassBox.SelectedIndex, manageClassesCampusBox.SelectedItem.ToString(), manageClassesCursusBox.SelectedItem.ToString());
                SubjectBox.ItemsSource = bdd.GetSubjects();
                label13.Visibility = Visibility.Hidden;
                TeachersList.Visibility = Visibility.Hidden;
                TeacherInfos.Visibility = Visibility.Hidden;
                TeacherInfos.Content = "Add teacher to class: " + manageClassesClassBox.SelectedItem.ToString();
                TeacherList.ItemsSource = bdd.GetTeacher(manageClassesCampusBox.SelectedItem.ToString(), manageClassesClassBox.SelectedIndex);
            }
            catch { }
        }

        private void TeacherList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                int t = bdd.GetTeacherId(TeacherList.SelectedItem.ToString(), manageClassesCampusBox.SelectedItem.ToString(), manageClassesClassBox.SelectedIndex);
                NewUsers teacher = bdd.GetTeacherInfos(t);
                InfosTeacherID.Content = "ID: " + teacher.id.ToString();
                InfosTeacherName.Content = "Name: " + teacher.name;
                InfosTeacherSurname.Content = "Surname: " + teacher.firstname;
                InfosTeacherSubject.Content = "Subject: " + TeacherList.SelectedItem.ToString();
                ChangeTeacher.IsEnabled = true;
                
            }
            catch {
                InfosTeacherID.Content = "ID: ";
                InfosTeacherName.Content = "Name: ";
                InfosTeacherSurname.Content = "Surname: ";
                InfosTeacherSubject.Content = "Subject: ";
            }

            
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CultureInfo myCI = new CultureInfo("en-US");
                System.Globalization.Calendar myCal = myCI.Calendar;
                int curweek = myCal.GetWeekOfYear(new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day), CalendarWeekRule.FirstFullWeek, DayOfWeek.Monday);
                timeTableGridAdmin.ItemsSource = bdd.GetPlanning(manageClassesCampusBox.SelectedItem.ToString(), manageClassesCursusBox.SelectedItem.ToString(), curweek, manageClassesClassBox.SelectedIndex, DateTime.Now.Year);
                managePlanningTab.Focus();
            }
            catch { }
        }

        private void AddTeacher_Click(object sender, RoutedEventArgs e)
        {
           
           string sub = Interaction.InputBox("New subject", "New subject", "", -1, -1);
           if ( sub != "" ) {
            bdd.addSubject(sub, manageClassesCursusBox.SelectedItem.ToString(),manageClassesCampusBox.SelectedItem.ToString(),manageClassesClassBox.SelectedIndex);
           TeacherList.ItemsSource = bdd.GetTeacher(manageClassesCampusBox.SelectedItem.ToString(), manageClassesClassBox.SelectedIndex);
           SubjectBox.ItemsSource = bdd.GetSubjects();
           }
           
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                bdd.delSubject(TeacherList.SelectedItem.ToString());
                TeacherList.ItemsSource = bdd.GetTeacher(manageClassesCampusBox.SelectedItem.ToString(), manageClassesClassBox.SelectedIndex);
            }
            catch { }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
          
                try
                {
                    bdd.delSubject(TeacherList.SelectedItem.ToString(), manageClassesClassBox.SelectedIndex, manageClassesCampusBox.SelectedItem.ToString());
                    TeacherList.ItemsSource = bdd.GetTeacher(manageClassesCampusBox.SelectedItem.ToString(), manageClassesClassBox.SelectedIndex);
                }
                catch { }
            
        }

        private void SubjectBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            label13.Visibility = Visibility.Visible;
            TeachersList.Visibility = Visibility.Visible;
            TeacherInfos.Visibility = Visibility.Visible;
            try { TeachersList.ItemsSource = bdd.GetTeachers(); }
            catch { }
        }

        private void applyButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                applyButton.IsEnabled = false;
                bdd.addSubject(SubjectBox.SelectedItem.ToString(),manageClassesCursusBox.SelectedItem.ToString(), manageClassesCampusBox.SelectedItem.ToString(), manageClassesClassBox.SelectedIndex, Convert.ToInt32(TeachersList.SelectedItem.ToString()));
                TeacherList.ItemsSource = bdd.GetTeacher(manageClassesCampusBox.SelectedItem.ToString(), manageClassesClassBox.SelectedIndex);
            }
            catch { }
        }

        private void TeachersList_SelectionChanged(object sender, SelectionChangedEventArgs e) //erreur là
        {
            try
            {
                NewUsers teacher = bdd.GetTeacherInfos(Convert.ToInt32(TeachersList.SelectedItem.ToString()));
                TeacherInfos.Content = teacher.name + " " + teacher.firstname;
                applyButton.IsEnabled = true;
            }
            catch { }
        }

        private void ChangeTeacher_Click(object sender, RoutedEventArgs e)
        {
            TeacherID.IsEnabled = true;
            TeacherID.ItemsSource = bdd.GetTeachers();
        }

        private void TeacherID_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string sub = TeacherList.SelectedItem.ToString();
            ChangeTeacher.IsEnabled = true;
            TeacherID.IsEnabled = false;
            int teacherID = Convert.ToInt32(TeacherID.SelectedItem.ToString());
            bdd.modifyTeacher(manageClassesCursusBox.SelectedItem.ToString(), manageClassesCampusBox.SelectedItem.ToString(), TeacherList.SelectedItem.ToString(), teacherID);
            TeacherList.SelectedIndex = 0;
            TeacherList.SelectedItem = sub; 

        }

        private void SubjectList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                textBox1.IsEnabled = true;
                textBox1.Text = bdd.GetHoursFromSubject(SubjectList.SelectedItem.ToString(), CampusList1.SelectedItem.ToString()).ToString();
                int i = bdd.countHoursAllocatedToSubject(SubjectList.SelectedItem.ToString(), CampusList1.SelectedItem.ToString());
                HoursAllocted.Content = "Hours allocated to this class: " + i;
            }
            catch { }
            
        }

        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            ApplyHours.IsEnabled = true; 
        }

        private void ApplyHours_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                bdd.modifyHours(Convert.ToInt32(textBox1.Text), SubjectList.SelectedItem.ToString(), CampusList1.SelectedItem.ToString());
                
                ApplyHours.IsEnabled = false;
            }
            catch { MessageBox.Show("Error: Please, enter a integer in the textbox"); }
        }

        private void managePlanningTab_Loaded(object sender, RoutedEventArgs e)
        {
            comboBoxManagePlanningCursus.ItemsSource = bdd.getCursus();
            comboBoxManagePlanningCampus.ItemsSource = bdd.GetCampus();
        }

        private void CampusList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

    }   
}